package com.dxq.test.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dxq.test.App;
import com.dxq.test.common.AppManager;
import com.dxq.test.common.Constant;
import com.dxq.test.R;
import com.dxq.test.db.TablesDao;

/**
 * Author: zsh
 * Date: 2023/11/13
 * Describe: 登录页面
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;

    private ImageView ivBack;
    private TextView tvTitle;
    private Button btnLogin;
    private EditText etUsername, etPassword;

    private String sex = "";
    private String breedingStatus = "";
    private String QRCodeID = "";
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_login);
        //tablesDao = new TablesDao(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        tvTitle.setText("登录");
        ivBack.setVisibility(View.GONE);

        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btnLogin:
                login();
                break;
        }
    }


    /**
     * 登录
     */
    private void login() {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        if (username.equals("")) {
            ToastUtils.showShort("先输入账号");
            return;
        }
        if (password.equals("")) {
            ToastUtils.showShort("先输入密码");
            return;
        }
        if (!username.equals("admin")) {
            ToastUtils.showShort("输入账号错误");
            return;
        }
        if (!password.equals("admin123")) {
            ToastUtils.showShort("输入密码错误");
            return;
        }
        //保存登录人信息
        SPUtils.getInstance().put(Constant.loginUsername, username);
        if (App.select_pasture_id <= 0) {
            Intent intent = new Intent(LoginActivity.this, PastureActivity.class);
            intent.putExtra("isFirst", true);
            startActivity(intent);
        } else {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }
        this.finish();
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
